 /**
 * 
 */
package com.aspire.android.heliar.raw.mms;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author panglei
 * 
 */
public class MMSManager {

	private Map<Integer, MMS> mmss = new HashMap<Integer, MMS>();
	private List<MMS> headlineList = new LinkedList<MMS>();

	private static MMSManager instance = new MMSManager();

	private MMSManager() {
		reload();
	}

	public static MMSManager getInstance() {
		return instance;
	}

	public void reload() {
		List<MMS> mmss = MMSParser.listMMS(true);
		for (MMS mms : mmss) {
			this.mmss.put(mms.getId(), mms);
		}
	}

	public List<MMS> listMMS() {
		if (mmss.size() != headlineList.size()) {
			headlineList.clear();
			Set<Integer> ids = mmss.keySet();
			for (Integer id : ids) {
				headlineList.add(mmss.get(id));
			}
		}
		return headlineList;
	}

	public MMS getMMS(int id) {
		MMS mms = mmss.get(id);

		if (mms == null) {
			return null;
		}

		if (mms.getFrames().size() == 0) {
			loadMMS(mms);
		}

		return mms;
	}

	private MMS loadMMS(MMS mms) {
		MMSParser.parseMMS(mms.getId(), mms);
		return mms;
	}
}
